<!DOCTYPE html>
<html>
  <head>
    <title>
      cycle-connection-gc.html
    </title>
    <script src="../../resources/gc.js"></script>
    <script src="../../resources/js-test.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      description('Cycles of AudioNode connections should be collected.');
      window.jsTestIsAsync = true;

      let context;
      let initialCount;

      (async () => {
        await asyncGC();

        context = new OfflineAudioContext(2, 1024, 44100);
        initialCount = internals.audioHandlerCount();
        createCycle();
        debug('A cycle was created:');
        shouldBeTrue('internals.audioHandlerCount() > initialCount');

        // Need to render to cleanup the cycle on an offline context
        await context.startRendering();

        await asyncGC();
        debug('GC happened:');
        shouldBe('internals.audioHandlerCount()', 'initialCount');
        finishJSTest();
      })();

      function createCycle() {
        let source = context.createBufferSource();
        let delay1 = context.createDelay(1 / context.sampleRate);
        let delay2 = context.createDelay(1 / context.sampleRate);
        source.connect(delay1);
        delay1.connect(delay2);
        delay2.connect(delay1);
        delay1.connect(context.destination);
      }
    </script>
  </body>
</html>
